package fr.labri.macchiato.ui.project;

import java.util.ArrayList;
import java.util.List;

import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.IconButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import fr.labri.macchiato.core.model.Project;
import fr.labri.macchiato.core.service.Services;
import fr.labri.macchiato.ui.ApplicationPanel;

/**
 * A window providing a "browse" button and a drag'n drop area in order to
 * upload a project
 * 
 * @author Matthieu FOUCAULT
 * 
 */
public class ProjectLoader extends Window {

	private ApplicationPanel appPanel;
	private HTMLFlow dropArea;
	private List<Project> projects;

	private HLayout buttonsLayout;
	private VLayout projectLayout;
	private IconButton validateButton, cancelButton;

	/**
	 * Creates and adds a project loader to the application panel
	 * 
	 * @param appPanel
	 */
	public ProjectLoader(final ApplicationPanel appPanel) {
		super();
		projects = new ArrayList<Project>();
		this.appPanel = appPanel;

		setCanDrag(false);
		setShowMaximizeButton(false);
		setShowMinimizeButton(false);

		setTitle("Charger des projets existants");
		setWidth("50%");
		setHeight100();
		setIsModal(true);
		setShowModalMask(true);
		setAutoCenter(true);

		dropArea = new HTMLFlow(
				"<div id=\"holder\">Déposez les fichiers ici</div>");
		addItem(dropArea);

		projectLayout = new VLayout(10);
		projectLayout.setMargin(20);
		projectLayout.setWidth100();
		projectLayout.setAutoHeight();
		addItem(projectLayout);

		buttonsLayout = new HLayout(15);
		addItem(buttonsLayout);

		validateButton = new IconButton("Valider");
		validateButton.setLargeIcon("/icons/validIcon.svg");
		validateButton.setOrientation("vertical");
		validateButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				for (Project project : projects) {
					Services.getInstance().getProjectService()
							.deleteProject(project.getId());
					Services.getInstance().getProjectService()
							.newProject(project);

					appPanel.setProjectWidget(new ProjectWidget(project.getName()));
				}
				destroy();
			}
		});

		buttonsLayout.addMember(validateButton);

		cancelButton = new IconButton("Annuler");
		cancelButton.setLargeIcon("/icons/cancelIcon.png");
		cancelButton.setOrientation("vertical");
		cancelButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				destroy();

			}
		});
		buttonsLayout.addMember(cancelButton);
		buttonsLayout.setMargin(30);
		show();
		handleDragNDrop();

	}

	private native void handleDragNDrop() /*-{
		var projectLoader = this;
		var holder = $doc.getElementById('holder');

		holder.ondragover = function() {
			this.className = 'hover';
			return false;
		};
		holder.ondragend = function() {
			this.className = '';
			return false;
		};
		holder.ondrop = function(e) {
			this.className = '';
			e.preventDefault();

			var file = e.dataTransfer.files[0], reader = new FileReader();
			reader.onload = function(event) {

				projectLoader.@fr.labri.macchiato.client.project.ProjectLoader::uploadProject(Ljava/lang/String;)(event.target.result);
			};

			reader.readAsText(file);

			return false;
		};
	}-*/;

//	private void uploadProject(String jsonProject) {
//		try {
//			Project project = projectEncoder.decode(jsonProject);
//			addProject(project);
//		} catch (Exception e) {
//			SC.say("Erreur", e.getLocalizedMessage());
//		}
//
//	}

	private void addProject(final Project project) {
		projects.add(project);
		final HLayout labelLayout = new HLayout(10);
		Label projectLabel = new Label("Projet: " + project.getName());
		projectLabel.setWidth("*");
		projectLabel.setStyleName("projectLabel");
		IconButton deleteButton = new IconButton("");
		deleteButton.setIcon("/icons/delete.png");
		deleteButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				projects.remove(project);
				projectLayout.removeMember(labelLayout);

			}
		});
		
		labelLayout.addMember(deleteButton);
		labelLayout.addMember(projectLabel);
		projectLayout.addMember(labelLayout);
		labelLayout.setWidth100();
	}

}
